#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
 struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
    
};
class Solution {
public:
    TreeNode* _build(vector<int>& inorder, int inbegin, int inend, vector<int>& preorder, int& mid)
    {
        cout << "( " << inbegin << " " << inend << " )";
        if (inbegin > inend)
        {
            return nullptr;
        }
        int i = 0;
        for (i = inbegin;inorder[i] != preorder[mid];i++);
        TreeNode* newNode = new TreeNode(inorder[i]);
        cout << i << " ";
        ++mid;
        newNode->left = _build(inorder, inbegin, i - 1, preorder, mid);
        newNode->right = _build(inorder, i + 1, inend, preorder, mid);
        return newNode;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
    {
        int inend = inorder.size() - 1;
        int mid = 0;
        return _build(inorder, 0, inend, preorder, mid);
    }
};